home *** CD-ROM | disk | FTP | other *** search
- Path: news.cc.sunysb.edu!ghauser
- From: ghauser@ic.sunysb.edu (George Hauser)
- Newsgroups: comp.lang.c
- Subject: Need help with a STRING that won't go away!!!
- Date: 29 Mar 1996 17:42:26 GMT
- Organization: State University of New York at Stony Brook
- Message-ID: <4jh7e2$jjr@abel.cc.sunysb.edu>
- NNTP-Posting-Host: libws4.cc.sunysb.edu
-
-
- Hi...
-
- I have the following problem. This program is supposed to do
- some text formating so that we can bring data into our database
- without problems.
-
- Tha part works fine... it may not be the best way (I always get new
- suggestions) but it works Ok.
-
- Now, the great pain in the butt is what follows:
-
- In this program I am interested in processing unti the EOF, when
- I get to it I don't want to loop again to avoid dumping the
- data that still is in the string (line).
-
- As it is, it dumps the exact same record that was on the last line twice!
- This causes the import to POST an OCEAN FREIGHT amount 2x!!!
-
- This is really bad.
-
- So what I did as a quick fix was to reset the line to be full of NULLS
-
- I figured this do nothing. fputs with nulls should write nothing....
-
- WRONG it is writting an extra line (or at least a EOL (\n)).
-
- This causesxs another problem.... we import a empty record
- into our database which causes the sequential assignment of numbers
- to get all screwed up.... pretty bad also.
-
- How in the world can I prevent t code from executing this fputs
- when the next char is the EOL? I remember in pascal you can look
- at the next char without actually moving the file pointer. Is there
- something like that in C? this way I could say something like.
-
- -- If nextchar is EOL(\n) then skip the fputs
-
- I have tried getting the lenght strlen to prevent writing if it is
- 0 but a line full of nulls does not return 0!!
-
- I'm sure there is a simple way to check for this but I am stuck...
- and I have an ugly procedure in my database that checks for empty
- records... which sucks because this problem should be taken care of here
- before creating the new file.
-
-
- Thanks so much and I live the main() so that you know what I am talking about.
-
- - George
-
- (ps. Any help will be appreciated! ghauser@ic.sunysb.edu)
-
-
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
-
- #define linemax 194 /* The predifined lenght of the record */
-
- --- All previous functions and file openings are coded here.
-
- records = 0;
- while(!feof(sourcefp))
- {
- if (ferror(sourcefp))
- {
- printf("\nError while reading source file.\n");
- exit(1);
- }
- fgets(line,linemax,sourcefp); /* Get Bad Record */
- rmgarbage(line); /* Remove bad chars */
- fillrecords(line); /* Format the record line */
-
- /* here I tried checking for empty lines */
- fputs(line,destfp); /* Write to disk */
-
- records++; /* Increment our counter */
-
- /* here I was setting the line[] to '\0' */
-
- }
- fclose(sourcefp); /* Close source file. */
- fclose(destfp); /* Close destination file. */
- printf("Total Number Of Records Processed: %d \n", records);
- } /* End */
-